#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'limin'

import threading
import PySimpleGUI as sg
from tqsdk import TqApi, TargetPosTask

api = TqApi()
quote_a = api.get_quote("SHFE.ss2011")
quote_b = api.get_quote("SHFE.ss2012")


class WorkingThread(threading.Thread):
    def run(self):
        while True:
            api.wait_update()


# 创建新线程
wt = WorkingThread()
wt.start()

layout = [[sg.Text('ss2011'), sg.Text("99999", key="ss2009.bp1"), sg.Text("99999", key="ss2009.sp1")],
          [sg.Text('ss2012'), sg.Text("99999", key="ss2010.bp1"), sg.Text("99999", key="ss2010.sp1")],
          [sg.Text('买入价差'), sg.Text("99999", key="buy_spread")],
          [sg.Text('卖出价差'), sg.Text("99999", key="sell_spread")],
          ]

window = sg.Window('价差显示', layout)

while True:  # Event Loop
    event, values = window.Read(timeout=1)
    if event is None or event == 'Exit':
        break
    window.Element('ss2009.bp1').Update(quote_a.bid_price1)
    window.Element('ss2009.sp1').Update(quote_a.ask_price1)
    window.Element('ss2010.bp1').Update(quote_b.bid_price1)
    window.Element('ss2010.sp1').Update(quote_b.ask_price1)
    window.Element('buy_spread').Update(quote_a.ask_price1 - quote_b.bid_price1)
    window.Element('sell_spread').Update(quote_a.bid_price1 - quote_b.ask_price1)

window.Close()
